﻿// -----------------------------------------------------------------------
// <copyright file="DbErrorCode.cs" company="JSC Centido">
//  Copyright (c) JSC Centido. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace Centido.Core
{
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    ///   API specific database error codes.
    /// </summary>
    /// <remarks>
    ///   Some database errors may result from the API validation logic.
    ///   In that case errors can be distinguished be the their error codes.
    /// </remarks>
    /// <example>
    ///   The following example  demonstrates how to obtain an API specific database error code.
    ///   <code>
    ///     using System;
    ///     using System.Data.Common;
    ///     using Centido.Core;
    /// 
    ///     class ErrorCodeSample
    ///     {
    ///         static void Main()
    ///         {
    ///             var domainConfig = new DomainConfiguration()
    ///             {
    ///                 DbConnectionString = @"Driver={SQL Server};Server=.\SQLEXPRESS;Database=MyDb;Uid=admin;Pwd=P@ssw0rd;",
    ///                 DbProviderName = "MSSQLSERVER"
    ///             };
    ///         
    ///             var domain = Domain.Build(domainConfig);
    ///         
    ///             domain.DeleteAll();
    ///         
    ///             //Adding a new table
    ///             var table = domain.AddTable("my_table");
    ///         
    ///             //Granting insert permissions
    ///             table.Grant(DataAction.Insert);
    ///             table.Grant(DataAction.Select);
    ///             table.Grant(DataAction.Update);
    ///             table.Grant(DataAction.Delete, AccessLevel.Own);
    /// 
    ///             //Inserting a row
    ///             var id = table.InsertRow();
    ///         
    ///             //Changing the owner of the row to someone else
    ///             table.UpdateRow(id, table.SystemColumns.Owner.Value("someone_else"));
    ///         
    ///             //Trying to delete this row
    ///             try
    ///             {
    ///                 table.DeleteRow(id);
    ///             }
    ///             catch(DbException e)
    ///             {
    ///                 switch(e.GetErrorCode())
    ///                 {
    ///                     case DbErrorCode.InsufficientPrivilegesToDeleteTheRow:
    ///                         Console.WriteLine("Insufficient privileges to delete the row.");
    ///                         break;
    ///                     case DbErrorCode.DeletingWhenThereAreRefsWithFalseCascadeDelete:
    ///                         Console.WriteLine("Deleting when there references with false cascade delete.");
    ///                         break;
    ///                     case DbErrorCode.None:
    ///                         Console.WriteLine("Unknown error.");
    ///                         break;
    ///                 }
    ///             }
    ///         }
    ///     }    
    ///   </code>
    /// </example>
    [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
    public enum DbErrorCode
    {
        /// <summary>
        ///   Deleting when there are references on the row with cascade delete set to false.
        /// </summary>
        DeletingWhenThereAreRefsWithFalseCascadeDelete = 20001,

        /// <summary>
        ///   Restoring a row when there is no row that was referenced by it.
        /// </summary>
        RestoringWhenThereIsNoReferencedRow = 20002,

        /// <summary>
        ///   Insufficient privileges to update the row.
        /// </summary>
        InsufficientPrivilegesToUpdateTheRow = 20003,

        /// <summary>
        ///   Insufficient privileges to delete the row.
        /// </summary>
        InsufficientPrivilegesToDeleteTheRow = 20004,

        /// <summary>
        ///   Deleting a system column.
        /// </summary>
        DeletingSystemColumn = 20005,

        /// <summary>
        ///   Renaming a system column.
        /// </summary>
        RenamingSystemColumn = 20006,

        /// <summary>
        ///   Renaming a column is not supported by the dbms.
        /// </summary>
        RenamingColumnIsNotSupportedByDbms = 20007,

        /// <summary>
        ///   Changing a system property of a system column.
        /// </summary>
        ChangingSystemPropertyOfSystemColumn = 20008,

        /// <summary>
        ///   Adding a unit with a duplicate name.
        /// </summary>
        AddingUnitWithDuplicateName = 20017,

        /// <summary>
        ///   Deleting a nonexistent enum member.
        /// </summary>
        DeletingNonexistentEnumMember = 20018,

        /// <summary>
        ///   Column was not found.
        /// </summary>
        ColumnWasNotFound = 20019,

        /// <summary>
        ///   Adding a column with a duplicate id.
        /// </summary>
        AddingColumnWithDuplicateId = 20020,

        /// <summary>
        ///   Adding a column with a duplicate name.
        /// </summary>
        AddingColumnWithDuplicateName = 20021,

        /// <summary>
        ///   Adding a duplicate reference target.
        /// </summary>
        AddingDuplicateRefTarget = 20022,

        /// <summary>
        ///   Deleting a nonexistent reference target.
        /// </summary>
        DeletingNonexistentRefTarget = 20023,

        /// <summary>
        ///   Referenced row was not found.
        /// </summary>
        ReferencedRowWasNotFound = 20024,

        /// <summary>
        ///   Adding a table with a duplicate id.
        /// </summary>
        AddingTableWithDuplicateId = 20025,

        /// <summary>
        ///   Adding a table with a duplicate name.
        /// </summary>
        AddingTableWithDuplicateName = 20026,

        /// <summary>
        ///   Table was not found.
        /// </summary>
        TableWasNotFound = 20027,

        /// <summary>
        ///   Adding an index with a duplicate id.
        /// </summary>
        AddingIndexWithDuplicateId = 20028,

        /// <summary>
        ///   Adding an index with a duplicate column set.
        /// </summary>
        AddingIndexWithDuplicateColumnSet = 20029,

        /// <summary>
        ///   Deleting a nonexistent index.
        /// </summary>
        DeletingNonexistentIndex = 20030,

        /// <summary>
        ///   Adding a unit with a duplicate id.
        /// </summary>
        AddingUnitWithDuplicateId = 20031,

        /// <summary>
        ///   Specified unit was not found.
        /// </summary>
        UnitWasNotFound = 20032,

        /// <summary>
        ///   Building a cycle of units.
        /// </summary>
        BuildingCycleOfUnits = 20033,

        /// <summary>
        ///   Adding a role with a duplicate name.
        /// </summary>
        AddingRoleWithDuplicateName = 20034,

        /// <summary>
        ///   Specified role was not found.
        /// </summary>
        RoleWasNotFound = 20035,

        /// <summary>
        ///   Specified user was not found.
        /// </summary>
        UserWasNotFound = 20036,

        /// <summary>
        ///   Adding a user in a role twice.
        /// </summary>
        AddingUserInRoleTwice = 20037,

        /// <summary>
        ///   Adding a role in role twice.
        /// </summary>
        AddingRoleInRoleTwice = 20038,

        /// <summary>
        ///   User enrollment was not found.
        /// </summary>
        UserEnrollmentWasNotFound = 20039,

        /// <summary>
        ///   Role enrollment was not found.
        /// </summary>
        RoleEnrollmentWasNotFound = 20040,

        /// <summary>
        ///   Adding a user with a duplicate name.
        /// </summary>
        AddingUserWithDuplicateName = 20041,

        /// <summary>
        ///   Changing the current user logging ability.
        /// </summary>
        ChangingCurrentUserCanLogon = 20042,

        /// <summary>
        ///   Changing the current user ability to change security settings.
        /// </summary>
        ChangingCurrentUserCanChangeSecurity = 20043,

        /// <summary>
        ///   Current user has no permission to change security settings.
        /// </summary>
        CurrentUserHasNoPermissionToChangeSecurity = 20044,

        /// <summary>
        ///   Insufficient privileges to change security settings permissions.
        /// </summary>
        InsufficientPrivilegesToChangeSecurityPermissions = 20045,

        /// <summary>
        ///   Deleting the current user.
        /// </summary>
        DeletingCurrentUser = 20046,

        /// <summary>
        ///   Specified principal was not found.
        /// </summary>
        PrincipalWasNotFound = 20047,

        /// <summary>
        ///   Adding a duplicate table permission.
        /// </summary>
        AddingDuplicateTablePermission = 20048,

        /// <summary>
        ///   Specified table permission was not found.
        /// </summary>
        TablePermissionWasNotFound = 20049,

        /// <summary>
        ///   Adding a duplicate unit parent.
        /// </summary>
        AddingDuplicateUnitParent = 20050,

        /// <summary>
        ///   Specified unit parent was not found.
        /// </summary>
        UnitParentWasNotFound = 20051,

        /// <summary>
        ///   Adding a duplicate user unit.
        /// </summary>
        AddingDuplicateUserUnit = 20052,

        /// <summary>
        ///   User unit was not found.
        /// </summary>
        UserInUnitWasNotFound = 20053,

        /// <summary>
        ///   Adding a duplicate enumeration member.
        /// </summary>
        AddingDuplicateEnumMember = 20054,

        /// <summary>
        ///   Breaking column values uniqueness.
        /// </summary>
        BreakingUniqueness = 20055,

        /// <summary>
        ///   Unknown error
        /// </summary>
        None = 0
    }
}